﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Jzy.Workflow.Code
{
    public class Flow
    {
        private List<FlowLine> _lines;
        private List<FlowNode> _nodes;

        private Canvas _canvas;

        public int _flowID
        {
            get;
            set;
        }

        public Flow(int flowID, Canvas cavas)
        {
            _flowID = flowID;
            _canvas = cavas;
            _lines = new List<FlowLine>();
            _nodes = new List<FlowNode>();
        }


        public void Init()
        {
            DataService.FlowSoapClient flowData = DataFactory.Create();

            flowData.GetWorkFlowCompleted += new EventHandler<Jzy.Workflow.DataService.GetWorkFlowCompletedEventArgs>(flowData_GetWorkFlowCompleted);

            
            flowData.GetWorkFlowAsync(_flowID);

            //FlowNode start = new FlowNode("开始(拟稿)");
            //start.Type = NodeType.开始;
            //FlowNode end = new FlowNode("结束");
            //end.Type = NodeType.结束;
            //FlowNode node1 = new FlowNode("部门经理审批");
           
            //FlowNode node2 = new FlowNode("节点2");
            //FlowNode node3 = new FlowNode("节点3");

            //FlowLine line1 = new FlowLine(start, node1);
            //FlowLine line2 = new FlowLine(node1, node2);
            //FlowLine line3 = new FlowLine(node2, node3);
            //FlowLine line4 = new FlowLine(node3, end);
            //FlowLine line5 = new FlowLine(node1, node3);
            //FlowLine line6 = new FlowLine(node1, end);

            //_nodes.Add(start);
            //_nodes.Add(node1);
            //_nodes.Add(node2);
            //_nodes.Add(node3);
            //_nodes.Add(end);

            //_lines.Add(line1);
            //_lines.Add(line2);
            //_lines.Add(line3);
            //_lines.Add(line4);
            //_lines.Add(line5);
            //_lines.Add(line6);

            //RenderFlow();
        }

        void flowData_GetWorkFlowCompleted(object sender, Jzy.Workflow.DataService.GetWorkFlowCompletedEventArgs e)
        {

            XElement xml = XElement.Parse(e.Result);
            IEnumerable<XElement> nodes = xml.Element("Nodes").Elements("Node");
           
            foreach (XElement node in nodes)
            {
                _nodes.Add(FlowNode.Parse(node));
            }

            IEnumerable<XElement> lines = xml.Elements("Lines").Elements("Line");
            foreach (XElement line in lines)
            {
                _lines.Add(FlowLine.Parse(line, _nodes));
            }

            RenderFlow();
        }

        private void RenderFlow()
        {
            _canvas.Children.Clear();
            for (int i = 0; i < _nodes.Count; i++)
            {
                _canvas.Children.Add(_nodes[i].Node);
            }

            for (int i = 0; i < _lines.Count; i++)
            {
                _canvas.Children.Add(_lines[i].Line);
                _lines[i].Draw();
            }
        }
    }
}
